package cn.bdqn.service.impl;

import cn.bdqn.dao.AirDao;
import cn.bdqn.dao.impl.AirDaoImpl;
import cn.bdqn.entity.Air;
import cn.bdqn.service.AirService;
import cn.bdqn.util.PageBean;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;


public class AirServiceImpl implements AirService {

    private AirDao airDao = new AirDaoImpl();

    @Override
    public PageBean findAirByConditionAndByPage(
            Integer currentPage,
            Integer pageSize,
            Integer districtId) {
        try {
            //1. 查询数据的总条数.
            Integer totalCount = airDao.findCountByCondition(districtId);
            //2. 创建PageBean.
            PageBean pageBean = new PageBean(currentPage,pageSize,totalCount);
            //3. 查询具体显示的数据,list.
            List<Air> list = airDao.findByCondition(districtId,
                    pageBean.getStartIndex(),
                    pageBean.getPageSize());
            //4. 将数据set到PageBean中
            pageBean.setList(list);
            //5. 返回数据.
            return pageBean;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
        }
    }


    //添加
    @Override
    public Integer save(Air air) {
        try {
            //1. 封装数据.
            air.setLastModifyTime(new Date());
            //2. 调用dao保存
            Integer count = airDao.save(air);
            //3. 返回数据
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        } finally {
        }
    }


    // 根据id查询空气质量信息
    @Override
    public Air findById(Integer id) {
        try {
            Air air = airDao.findById(id);
            return air;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        } finally {
        }
    }

    // 修改空气质量信息.
    @Override
    public Integer updateById(Air air) {
        try {
            //1. 将最后修改时间属性赋值为当前系统时间.
            air.setLastModifyTime(new Date());

            //2. 调用dao修改.
            Integer count = airDao.update(air);

            //3. 返回数据
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        } finally {
        }
    }

    @Override
    public Integer deleteById(Integer id) {
        try {
            Integer count = airDao.deleteById(id);
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        } finally {
        }
    }
}
